/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.order;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrder;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrderAfter;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.hk.shop.service.order.HkShopUserOrderAfterService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 订单售后Controller
 *
 * @author 罗天文
 * @version 2018-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/order/hkShopUserOrderAfter")
public class HkShopUserOrderAfterController extends BaseController {

    @Autowired
    private HkShopUserOrderAfterService hkShopUserOrderAfterService;

    @ModelAttribute
    public HkShopUserOrderAfter get(@RequestParam(required = false) String id) {
        HkShopUserOrderAfter entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopUserOrderAfterService.get(id);
        }
        if (entity == null) {
            entity = new HkShopUserOrderAfter();
        }
        return entity;
    }

    @RequiresPermissions("shop:order:hkShopUserOrderAfter:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopUserOrderAfter hkShopUserOrderAfter, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {

        if (StringUtils.isBlank(UserUtils.getShopId())) {
            addMessage(redirectAttributes, "登录超时，请重新登录");
            return "redirect:" + Global.getFrontPath();
        }

        HkShopUserOrder order = new HkShopUserOrder();
        HkUserShop shop = new HkUserShop();
        shop.setId(UserUtils.getShopId());
        order.setShop(shop);
        hkShopUserOrderAfter.setOrder(order);
        Page<HkShopUserOrderAfter> page = hkShopUserOrderAfterService.findPage(new Page<HkShopUserOrderAfter>(request, response), hkShopUserOrderAfter);
        model.addAttribute("page", page);
        return "hk/shop/order/hkShopUserOrderAfterList";
    }

    @RequiresPermissions("shop:order:hkShopUserOrderAfter:view")
    @RequestMapping(value = "form")
    public String form(HkShopUserOrderAfter hkShopUserOrderAfter, Model model) {
        model.addAttribute("hkShopUserOrderAfter", hkShopUserOrderAfter);
        return "hk/shop/order/hkShopUserOrderAfterForm";
    }

    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "save")
    public String save(HkShopUserOrderAfter hkShopUserOrderAfter, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkShopUserOrderAfter)) {
            return form(hkShopUserOrderAfter, model);
        }
        hkShopUserOrderAfterService.save(hkShopUserOrderAfter);
        addMessage(redirectAttributes, "保存订单售后成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }

    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "delete")
    public String delete(HkShopUserOrderAfter hkShopUserOrderAfter, RedirectAttributes redirectAttributes) {
        hkShopUserOrderAfterService.delete(hkShopUserOrderAfter);
        addMessage(redirectAttributes, "删除订单售后成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }


    /**
     * 同意退款
     *
     * @param hkShopUserOrderAfter
     * @param model
     * @return
     */
    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "agreeReimburse")
    public String agreeReimburse(HkShopUserOrderAfter hkShopUserOrderAfter, Model model, RedirectAttributes redirectAttributes) {
        hkShopUserOrderAfterService.agreeReimburse(hkShopUserOrderAfter);
        addMessage(redirectAttributes, "同意订单退款成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }


    /**
     * 拒绝退款
     *
     * @param hkShopUserOrderAfter
     * @param model
     * @return
     */
    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "refuseReimburse")
    public String refuseReimburse(HkShopUserOrderAfter hkShopUserOrderAfter, Model model, RedirectAttributes redirectAttributes) {
        hkShopUserOrderAfterService.refuseReimburse(hkShopUserOrderAfter);
        addMessage(redirectAttributes, "拒绝订单退款成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }


    /**
     * 同意退货
     *
     * @param hkShopUserOrderAfter
     * @param model
     * @return
     */
    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "agreeReturnOfGoods")
    public String agreeReturnOfGoods(HkShopUserOrderAfter hkShopUserOrderAfter, Model model, RedirectAttributes redirectAttributes) {

        hkShopUserOrderAfterService.agreeReturnOfGoods(hkShopUserOrderAfter);
        addMessage(redirectAttributes, "同意订单退货成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }


    /**
     * 拒绝退货
     *
     * @param hkShopUserOrderAfter
     * @param model
     * @return
     */
    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "refuseReturnOfGoods")
    public String refuseReturnOfGoods(HkShopUserOrderAfter hkShopUserOrderAfter, Model model, RedirectAttributes redirectAttributes) {
        hkShopUserOrderAfterService.refuseReturnOfGoods(hkShopUserOrderAfter);
        addMessage(redirectAttributes, "拒绝订单退货成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }


    /**
     * 提交商家收货信息
     *
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("shop:order:hkShopUserOrderAfter:edit")
    @RequestMapping(value = "logisticsMsg")
    public String logisticsMsg(HttpServletRequest request, RedirectAttributes redirectAttributes) {

        String orderNum = request.getParameter("orderNum");
        String ids = request.getParameter("ids");
        String receiptName = request.getParameter("receiptName");
        String receiptPhone = request.getParameter("receiptPhone");
        String provinceId = request.getParameter("province");
        String cityId = request.getParameter("city");
        String areaId = request.getParameter("area");
        String receipAddress = request.getParameter("receipAddress");
        hkShopUserOrderAfterService.logisticsMsg(orderNum, ids, receiptName, receiptPhone, provinceId, cityId, areaId, receipAddress);
        addMessage(redirectAttributes, "收货信息和同意退货提交成功");
        return "redirect:" + Global.getAdminPath() + "/shop/order/hkShopUserOrderAfter/?repage";
    }


}